package com.weishuang.monitor.leader.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.weishuang.monitor.leader.commons.entity.BaseEntity;
import com.weishuang.monitor.leader.commons.enums.SCODE;
import com.weishuang.monitor.leader.entity.ServersBaseInfo;
import com.weishuang.monitor.leader.entity.SysUser;
import com.weishuang.monitor.leader.mapper.ServersBaseInfoMapper;
import com.weishuang.monitor.leader.mapper.SysUserMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;

@Slf4j
@Service
public class EmailSendService {

    @Autowired
    private ServersBaseInfoMapper serversBaseInfoMapper;

    @Autowired
    private JavaMailSenderImpl javaMailSender;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Value("${spring.mail.username:null}")
    private String formUser;

    public void sendOffLineEmail(String sysId) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try{
                    ServersBaseInfo serversBaseInfo = serversBaseInfoMapper.selectById(sysId);
                    LambdaQueryWrapper<SysUser> eq = Wrappers.lambdaQuery(SysUser.class).eq(SysUser::getIsDefault, SCODE.NO_CODE.getValue())
                            .isNotNull(SysUser::getEmail).eq(BaseEntity::getStatus, SCODE.STATUS_NORMAL.getValue());
                    List<SysUser> sysUsers = sysUserMapper.selectList(eq);
                    if (sysUsers != null) {
                        HashSet<String> toEmail = new HashSet<>();
                        for (SysUser sysUser : sysUsers) {
                            toEmail.add(sysUser.getEmail());
                        }
                        String[] toEmails = new String[toEmail.size()];
                        toEmail.toArray(toEmails);
                        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
                        simpleMailMessage.setSubject("服务器离线提醒！");
                        simpleMailMessage.setText("您的一台"+serversBaseInfo.getSystemType()+"服务器："+serversBaseInfo.getSystemName()+"掉线了。请及时处理！");
                        simpleMailMessage.setTo(toEmails);
                        simpleMailMessage.setFrom(formUser);
                        javaMailSender.send(simpleMailMessage);
                    }
                }catch (Exception e){
                    log.error(e.getMessage(), e);
                }

            }
        }).start();
    }

    /**
     * 发送邮件
     * @param subject 标题
     * @param content 内容
     */
    public void sendEmail(String subject, String content) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try{
                    LambdaQueryWrapper<SysUser> eq = Wrappers.lambdaQuery(SysUser.class).eq(SysUser::getIsDefault, SCODE.NO_CODE.getValue())
                            .isNotNull(SysUser::getEmail).eq(BaseEntity::getStatus, SCODE.STATUS_NORMAL.getValue());
                    List<SysUser> sysUsers = sysUserMapper.selectList(eq);
                    if (sysUsers != null) {
                        HashSet<String> toEmail = new HashSet<>();
                        for (SysUser sysUser : sysUsers) {
                            toEmail.add(sysUser.getEmail());
                        }
                        String[] toEmails = new String[toEmail.size()];
                        toEmail.toArray(toEmails);
                        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
                        simpleMailMessage.setSubject(subject);
                        simpleMailMessage.setText(content);
                        simpleMailMessage.setTo(toEmails);
                        simpleMailMessage.setFrom(formUser);
                        javaMailSender.send(simpleMailMessage);
                    }
                }catch (Exception e){
                    log.error(e.getMessage(), e);
                }

            }
        }).start();
    }
}
